<?php

/**
 * MVM_MALL 网上商店系统 收汇宝接口
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-04-29 $
 * $Id: ctopay.class.php  www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL')){
    exit('Access Denied');
}
/*插件的代码必须和文件名保持一致 */
$payment['ctopay']['name']    = '收汇宝';

/*描述 */
$payment['ctopay']['desc']    = '收汇宝';

/* 支付费用 */
$payment['ctopay']['pay_fee'] = '0%';

/* 是否支持在线支付 */
$spayment['ctopay']['online']  = 0;

/*否支持货到付款*/
$payment['ctopay']['cod']     = 0;

//申请地址
$payment['ctopay']['reg']     = 'http://www.ctopay.com/';

/* 版权信息*/
$payment['ctopay']['license']  = '版权所有www.mvmmall.com';

/*接口需要的参数 */
$payment['ctopay']['cfg'] =array(
        array('name' => 'MerNo', 'value' => '','label'=>'商户号'),
        array('name' => 'MD5key', 'value' => '','label'=>'MD5私钥'),
    );

/**
 * 类
 */
class ctopay
{
    var $cfg;
    
    function ctopay($cfg = array())
    {
        foreach ($cfg AS $key=>$val)
        {
            $this->cfg[$val['name']] = $val['value'];
        }
    }
    /*提交支付请求*/
function pay_send($sn,$amount)	
	{
     $MD5key   = $this->cfg[MD5key];		//MD5私钥
     $MerNo    = $this->cfg[MerNo];					//商户号
     $BillNo   = $sn;		//订单号
     $Currency = "2";					//币种
     $Amount    = $amount;				//金额
     $DispAmount = $amount;					//外币金额
     $Language   = "1";					//语言
     $ReturnURL  = MVMMALL_DIR."/respond.php?code=".basename(__FILE__, '.class.php'); 			//返回地址
     $Remark     = "";  //备注
     $md5src = $MerNo.$BillNo.$Currency.$Amount.$Language.$ReturnURL.$MD5key;		//校验源字符串
     $MD5info = strtoupper(md5($md5src));		//MD5检验结果
	 $result="
				<FORM  name=re METHOD=post ACTION='http://payment.ctopay.com/payment/Interface' target='_blank'>
				<input type='hidden' name='MerNo' value='$MerNo'>
				<input type='hidden' name='Currency' value='$Currency'>
				<input type='hidden' name='BillNo' value='$BillNo'>
				<input type='hidden' name='Amount'  value='$Amount'>
				<input type='hidden' name='DispAmount' value='$DispAmount'>
				<input type='hidden' name='ReturnURL' value='$ReturnURL'>
				<input type='hidden' name='Language' value='$Language'>
				<input type='hidden' name='MD5info' value='$MD5info'>
				<input type='hidden' name='Remark' value='$Remark'>
				<input type='submit' value='联银通支付平台'>
				</FORM>
				";
		$result=trim($result);
		return   $result;
	}
    /**
     * 提交返回处理*
     */
    function pay_receive()
    {
    	//订单号
    	$BillNo = $_POST["BillNo"];
    	//币种
    	$Currency = $_POST["Currency"];
    	//银行ID号
    	$BankID = $_POST["BankID"];
    	//金额
    	$Amount = $_POST["Amount"];
    	//支付状态
    	$Succeed = $_POST["Succeed"];
    	//支付平台流水号
    	$TradeNo = $_POST["TradeNo"];
    	//支付结果
    	$Result = $_POST["Result"];
    	//取得的MD5校验信息
    	$MD5info = $_POST["MD5info"];
    	//备注
    	$Remark = $_POST["Remark"];
    	//支付人名称
    	$Drawee = $_POST["Drawee"];

    	//MD5私钥
    	$MD5key =  $this->cfg[MD5key];		//MD5私钥
    	//校验源字符串
    	$md5src = $BillNo.$Currency.$Amount.$Succeed.$MD5key;
    	//MD5检验结果
    	$md5sign = strtoupper(md5($md5src));
	
        if ($MD5info==$md5sign)
        {
            if ($Succeed == '1')
            {
                 //比较返回的订单号和金额与数据库中的金额是否相符
                $BillNo= strip_tags($BillNo);
                $list = order_info($BillNo);
                if ($list[order_amount]==$Amount){
                    /* 改变订单状态*/
                    change_order($BillNo);
                    return true;
                }else {
                    //和定单数据库不一致
                    return false;
                }
            }
        }
        else
        {
            return false;
        }
    }
}